<template>
  <div class="login">
    <el-form
      class="login-form"
      :model="loginForm"
      :rules="loginRules"
      ref="loginForm"
    >
      <h3 class="title">zhang-boot 后台管理系统</h3>
      <el-form-item prop="username">
        <el-input
          type="text"
          v-model="loginForm.username"
          auto-complete="off"
          placeholder="请输入用户账号"
        >
          <svg-icon
            slot="prefix"
            icon-class="user"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          type="password"
          v-model="loginForm.password"
          auto-complete="off"
          placeholder="请输入密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon
            slot="prefix"
            icon-class="password"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>

      <el-form-item prop="code">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="请输入验证码"
          style="width:63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon
            slot="prefix"
            icon-class="validCode"
            class="el-input__icon input-icon"
          />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img" />
        </div>
      </el-form-item>

      <el-checkbox
        v-model="loginForm.rememberMe"
        style="margin:0px 0px 20px 0px "
        >记住密码
      </el-checkbox>
      <el-form-item>
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%"
          @keyup.enter.native="handleLogin"
          @click="handleLogin"
        >
          <span v-if="!loading">登录</span>
          <span v-else>登录中...</span>
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getCodeImg, login } from '@/api/login'
import {
  setUserInfo,
  removeUserInfo,
  getUserInfo,
  setToken
} from '@/utils/cookie'
export default {
  name: 'login',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        code: '',
        rememberMe: false,
        uuid: ''
      },
      codeUrl: '',
      loading: false,
      redirect: undefined,
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '用户账号不能为空' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '密码不能为空' }
        ],
        code: [{ required: true, trigger: 'change', message: '验证码不能为空' }]
      }
    }
  },
  methods: {
    /**
     * 处理登录逻辑
     */
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            setUserInfo(this.loginForm)
          } else {
            removeUserInfo(this.loginForm)
          }
          this.loginForm.username = this.loginForm.username.trim()
          login(this.loginForm)
            .then(res => {
              console.log(res)
              setToken(res.token)
              let permissions = res.permissions
              this.$store.dispatch('setPermissions', permissions)
              this.loading = false
              const toPath = this.redirect || '/'
              this.$router.push({ path: toPath })
            })
            .catch(err => {
              this.loading = false
              this.getCode()
            })
        }
      })
    },
    /**
     * 获取验证码
     */
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = 'data:image/gif;base64,' + res.img
        this.loginForm.uuid = res.uuid
      })
    },
    /**
     * 获取登录信息
     */
    getLoginInfo() {
      this.loginForm = getUserInfo()
    }
  },
  created() {
    this.getLoginInfo()
    this.getCode()
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('../assets/image/login-background.jpg');
  background-size: cover;
}
.login .login-form {
  width: 400px;
  background-color: #fff;
  border-radius: 6px;
  padding: 25px;
  padding-bottom: 5px;
}
.login .login-form .title {
  margin: 0 auto 30px;
  text-align: center;
  font-size: 16px;
  color: #707070;
}
.login .login-form .login-code {
  width: 33%;
  float: right;
}
.login .login-form .login-code img {
  cursor: pointer;
  vertical-align: -2px;
}
.login .login-form .login-code .login-code-img {
  height: 40px;
}
</style>
